//
//  VisitorView.swift
//  DSWeiBo
//
//  Created by H on 16/4/12.
//  Copyright © 2016年 Mr.Lin. All rights reserved.
//

import UIKit

//在 swift 中如果定义协议
protocol VisitorViewDelegate:NSObjectProtocol
{
    //点击登录回调
    
    func loginBtnWillClick()
    
    func registerBtnWillClick()
}



class VisitorView: UIView {
    
    
    //定义一个属性保存代理对象
    //一定加上一个weak 避免循环引用
   weak var delegate: VisitorViewDelegate?
    
    /**
     设置未登录界面
     
     - parameter isHome:    是否是首页
     - parameter imageName: 需要展示的图标名称
     - parameter message:   需要展示的文本信息
     */
    func setupVisitorInfo(isHome:Bool,imageName:String,message:String)  {
        
        //如果不是首页  就隐藏转盘
        iconView.hidden = !isHome
        // 修改中间图标
        homeIcon.image = UIImage(named: imageName)
        //修改文本
        messageLabel.text = message
        
        
        // 首页开启动画
        if isHome {
            
            startAnimation()
        }
    }
    
    private func startAnimation(){
        //1.创建动画
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        //2.设置动画属性
        anim.toValue = 2 * M_PI_2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        //该属性默认为yes  代表动画只要执行完毕之后就移除
        anim.removedOnCompletion = false
        //3.将动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    func loginBtnClick()  {
        print(#function)
        delegate?.loginBtnWillClick()
    }
    
    func registerBtnClick() {
        
        print(#function)
        delegate?.registerBtnWillClick()

    }
    
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //1.添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //2.布局子控件
        //2.1设置背景
        
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.2 设置小房子
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        //2.3 设置文本
        messageLabel.xmg_AlignHorizontal(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        //"哪个控件"的“什么属性”“等于”“另外一个控件”的“什么属性”乘以”多少“ 加上“多少”
     let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        //2.4设置按钮
//        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: nil)
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100,height: 30), offset: CGPoint(x: 0,y: 20))
        loginButton.xmg_AlignVertical(type: XMG_AlignType.BottomRight,referView: messageLabel, size: CGSize(width: 100,height: 30), offset: CGPoint(x: 0,y: 20))
        //2.5设置蒙版
        maskBGView.xmg_Fill(self)
    }
    
    
    //swift推荐我们自定义一个控件 要么用纯代码 要么就是用xib/storyBoard
    required init?(coder aDecoder: NSCoder) {
        //如果通过xib 和 storyBoard创建该类 那么就会奔溃
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 懒加载控件
  
    /// 转盘
    private lazy var iconView:UIImageView = {
       
        let iv = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
        
        return iv
    }()
    /// 图标
    private lazy var homeIcon:UIImageView = {
       
        let iv = UIImageView(image:UIImage(named:"visitordiscover_feed_image_house"))
        
        return iv
    }()
    /// 文本
    private lazy var messageLabel:UILabel = {
       
        let label = UILabel()
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
        label.text = "**********************************************************"
        return label
    }()
    /// 登录按钮
    private lazy var loginButton:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: #selector(VisitorView.loginBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    /// 注册按钮
    private lazy var registerButton:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(VisitorView.registerBtnClick), forControlEvents: .TouchUpInside)

        
        return btn
    }()
    
    
    private lazy var maskBGView:UIImageView = {
       
        let iv = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
        
        return iv
    }()
}
